package com.lenovo.smarttraffic.zm.Map;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

/**
 * @author zm
 * @version 2.0
 * @date 2020/9/22 8:38
 */
public class MyMapActivivty extends BaseActivity {
    private MapView mMapView;
    private ImageView iv_fish;
    private ImageView iv_image;
    private TextView tv_change;
    private AMap aMap;
    private Boolean flage = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.mymap_layout;
    }

    private void initView() {
        mMapView = (MapView) findViewById(R.id.mMapView);
        iv_fish = (ImageView) findViewById(R.id.iv_fish);
        iv_fish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        iv_image = (ImageView) findViewById(R.id.iv_image);
        iv_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flage) {
                    String name1 = "1号小车 \n 联想大厦";
                    String name2 = "2号小车 \n 西铁营桥";
                    String name3 = "3号小车 \n G107";
                    String name4 = "4号小车 \n G2路段";
                    LatLng latLng = new LatLng(40.042737, 116.309884);
                    addMarker(40.042737, 116.309884, name1, BitmapDescriptorFactory.fromResource(R.mipmap.marker_one));
                    addMarker(39.860512, 116.347593, name2, BitmapDescriptorFactory.fromResource(R.mipmap.marker_second));
                    addMarker(39.802303, 116.209749, name3, BitmapDescriptorFactory.fromResource(R.mipmap.marker_thrid));
                    addMarker(39.847603, 116.472871, name4, BitmapDescriptorFactory.fromResource(R.mipmap.marker_forth));
                    aMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 10, 0, 30)), null);
                    tv_change.setText("1、2、3、4号小车的标记已完成");
                    flage = false;
                } else {
                    tv_change.setText("小车地图信息");
                    aMap.clear();
                    flage = true;
                }
            }
        });
        tv_change = (TextView) findViewById(R.id.tv_change);
    }

    private void addMarker(double v1, double v2, String name, BitmapDescriptor bitmapDescriptor) {
        LatLng latLng = new LatLng(v1, v2);
        MarkerOptions draggable = new MarkerOptions().position(latLng).title(name).icon(bitmapDescriptor).alpha(1).draggable(false);
        aMap.addMarker(draggable);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
}
